package com.hy.controller;

/**
 * @作者 坏银
 * @时间 2021/11/4 18:47
 * @作用 错题集控制层
 */

import cn.hutool.log.StaticLog;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hy.pojo.Sequence;
import com.hy.pojo.WrongSet;
import com.hy.service.impl.SequenceServiceImpl;
import com.hy.service.impl.WrongSetServiceImpl;
import com.hy.vo.Formulas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class wrongSet {

    @Resource
    private WrongSetServiceImpl wrongSetService;
    @Resource
    private SequenceServiceImpl sequenceService;

    /**
     * 查询错题集
     *
     * @return
     */
    @GetMapping("/wrongSet")
    public ArrayList<Formulas> toWrongSet() {
        QueryWrapper<WrongSet> wrapper = new QueryWrapper<>();
        wrapper.eq("id", 1);
        String querySet = wrongSetService.getOne(wrapper).getWset();
        //将查询到的id转化成为list集合
        ArrayList<Integer> parse = (ArrayList<Integer>) JSONUtils.parse(querySet);
        //定义返回的集合
        ArrayList<Formulas> formulas = new ArrayList<>();

        parse.forEach(id -> {
            Sequence sequence = sequenceService.getById(id);
            Formulas formula = new Formulas();
            //字符串转化
            if (sequence.getSymbol() == '/') {
                sequence.setSymbol('÷');
            } else if (sequence.getSymbol() == '*') {
                sequence.setSymbol('x');
            }
            //字符串拼接
            formula.setSequence(sequence.getForward() + " " + sequence.getSymbol() + " " + sequence.getBehind());
            formula.setAnswer(sequence.getAnswer());
            formula.setId(sequence.getId());
            formulas.add(formula);
        });
        return formulas;
    }

    /**
     * 通过id移除错题
     */
    @GetMapping("/removeWrong")
    public String getWrongSet(@RequestParam Integer id) {
        QueryWrapper<WrongSet> wrapper = new QueryWrapper<>();
        wrapper.eq("id", 1);
        String querySet = wrongSetService.getOne(wrapper).getWset();
        //将查询到的id转化成为list集合
        ArrayList<Integer> parse = (ArrayList<Integer>) JSONUtils.parse(querySet);
        HashSet<Integer> set = new HashSet<>(parse);
        set.remove(id);
        //判断是否为空，不为空就返回，为空就返回空set集合
        set = Optional.ofNullable(set).orElse(new HashSet<>());
        wrongSetService.updateById(new WrongSet(1, 1, JSONUtils.toJSONString(set)));
        StaticLog.info("移除错题成功");
        return "success";
    }

    /**
     * 移除全部错题
     */
    @GetMapping("/removeWrongAll")
    public String removeWrongAll() {
        wrongSetService.updateById(new WrongSet(1, 1, "[]"));
        StaticLog.info("移除错题成功");
        return "success";
    }

    /**
     * 添加错题
     */
    @GetMapping("/addWrong")
    public String sageWrongSet(@RequestParam Integer id) {
        QueryWrapper<WrongSet> wrapper = new QueryWrapper<>();
        wrapper.eq("id", 1);
        String querySet = wrongSetService.getOne(wrapper).getWset();
        //将查询到的id转化成为list集合
        ArrayList<Integer> parse = (ArrayList<Integer>) JSONUtils.parse(querySet);
        HashSet<Integer> set = new HashSet<>(parse);
        set.add(id);
        //判断是否为空，不为空就返回，为空就返回空set集合
        set = Optional.ofNullable(set).orElse(new HashSet<>());
        wrongSetService.updateById(new WrongSet(1, 1, JSONUtils.toJSONString(set)));
        StaticLog.info("添加错题成功！");
        return "success";
    }
}
